package com.ruoyi.net.service;

import com.jcraft.jsch.Session;
import com.ruoyi.common.ssh.SSHConnectionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SSHService {

    @Autowired
    private SSHConnectionManager connectionManager;

    // 创建SSH连接的服务方法
    public void createSSHConnection(String host, int port, String username, String password) throws Exception {
        connectionManager.createConnection(host, port, username, password);
    }

    // 根据主机名和端口号获取SSH连接对象的方法
    public Session getConnection(String host, int port) throws Exception {
        return connectionManager.getConnection(host, port);
    }


    // 执行Shell命令的服务方法
    public List<String> executeShellCommand(String host, int port, String command) throws Exception {
        return connectionManager.shellCommand(host, port, command);
    }

    // 执行SSH命令的服务方法
    public String executeSSHCommand(String host, int port, String command) throws Exception {
        return connectionManager.executeCommand(host,  port, command);
    }

    // 关闭指定SSH连接的服务方法
    public void closeCurrentConnections(String host, int port) throws Exception {
        connectionManager.closeCurrentConnections(host, port);
    }

    // 关闭所有SSH连接的服务方法
    public void closeAllSSHConnections() {
        connectionManager.closeAllConnections();
    }
}
